#pragma once

#include <atlsimpcoll.h>

namespace UTIL {

class CSV
{
public:
	static CSimpleArray<CString> ToArray(CString strCSV)
	{
		if (strCSV.Trim().IsEmpty())
			return CSimpleArray<CString>();

		CSimpleArray<CString> arResult;

		for (int idx = -1; (idx = strCSV.Find( _T(',') )) != -1; strCSV.Delete(0, idx + 1))
		{
			ATLASSERT(idx >= 0);

			if (idx <= 0)
				continue;

			CString strValue = strCSV.Left(idx);
			if (strValue.Trim().IsEmpty())
				continue;

			arResult.Add(strValue);
		}

		// Add last value
		if (!strCSV.Trim().IsEmpty())
			arResult.Add(strCSV);

		return arResult;
	}

	static CString ToString(const CSimpleArray<CString> &arValues)
	{
		if (!arValues.GetSize())
			return CString();

		CString strResult;
		for (int i = 0; i < arValues.GetSize(); ++i)
		{
			if (arValues[i].IsEmpty())
				continue;

			if (!strResult.IsEmpty())
				strResult += _T(",");

			strResult += arValues[i];
		}

		return strResult;
	}

private:
	CSV();
};

} // namespace UTIL